package com.lute.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ide.eclipse.quickfix.jdt.processors.RequestMappingDialog.Method;
import org.springframework.security.oauth2.common.json.JSONException;
import org.springframework.security.oauth2.common.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lute.json.UserNameJSON;
import com.lute.model.Client;
import com.lute.model.ClientAdminProfile;
import com.lute.model.User;
import com.lute.utils.LdapDAO;
import com.lute.utils.ServerErrorResponse;

@Controller
public class CheckUserLoginServlet {
	
	@Autowired
	LdapDAO ldap;
	
	@RequestMapping(value="/checkUserLogin", method= RequestMethod.POST, headers = "Content-type=application/json")
	public @ResponseBody String checkUserLogin(@RequestBody String dataJSON, HttpServletRequest request) throws JSONException {
		String result = "";
		String userLogin;
		JSONObject jsonReq;
		JSONObject jsonRes = new JSONObject();
		
		HttpSession session = request.getSession(false);
		if(session == null) {
			jsonRes.put("result", ServerErrorResponse.USER_NOT_LOGGED_IN.toString());
			result = jsonRes.toString();
			return result;
		}
		
		try {
			jsonReq = new JSONObject(dataJSON);
			userLogin = jsonReq.getString("userLogin");
		} catch(JSONException e) {
			jsonRes.put("result", ServerErrorResponse.MALFORMED_REQUEST.toString());
			result = jsonRes.toString();
			return result;
		}	
		
		String role = (String)session.getAttribute("role");
		
		/* check the authorization */
		if(!(role.equals("admin"))) {
			jsonRes.put("result", ServerErrorResponse.USER_NOT_ENTITLED.toString());
			result = jsonRes.toString();
			return result;
		}
		
		JSONObject basicUserData = new JSONObject();
		
		User user = User.getUserFromDB("login", userLogin);
		if(!(user == null)) {
			basicUserData.put("userId", user.getId_user());
			basicUserData.put("userFirstName", user.getFirst_name());
			basicUserData.put("userLastName", user.getLast_name());
		} else {
			user = (User)ldap.findUser(userLogin);
			if(user == null) {
				jsonRes.put("result", ServerErrorResponse.USER_NOT_FOUND.toString());
				result = jsonRes.toString();
				return result;
			} else {
				basicUserData.put("userId", JSONObject.NULL);
				basicUserData.put("userFirstName", user.getFirst_name());
				basicUserData.put("userLastName", user.getLast_name());
			}
		}
			
		jsonRes.put("result","OK");
		jsonRes.put("basicUserData", basicUserData);
		result = jsonRes.toString();
		return result;
	}
}
